package pl.grabar.clinic.core.dao;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

public abstract class CrudDaoService<T extends Serializable> implements CrudService<T> {

	protected Dao<T> dao;

	public CrudDaoService(Dao<T> dao) {
		this.dao = dao;
	}

	/**
	 * @throws  
	 * @see pl.xs.xdms.core.services.CrudService#create(java.lang.Object)
	 */
	@Override
	public void create(T t) {
		try {
			dao.save(t);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see pl.xs.xdms.core.services.CrudService#get(java.lang.String)
	 */
	@Override
	public T get(String id) {
		try {
			return dao.findById(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @see pl.xs.xdms.core.services.CrudService#getList()
	 */
	@Override
	public List<T> getList() {
		try {
			return dao.findAll();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @see pl.xs.xdms.core.services.CrudService#getList(java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.String)
	 */
	@Override
	public List<T> getList(Integer pageNumber, Integer pageSize, String sortProperty, String sortOrder) {
		try {
			return dao.findAll(pageNumber, pageSize, sortProperty, sortOrder, null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @see pl.xs.xdms.core.services.CrudService#update(java.lang.Object)
	 */
	@Override
	public void update(T t) {
		try {
			dao.update(t);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see pl.xs.xdms.core.services.CrudService#delete(java.lang.Object)
	 */
	@Override
	public void delete(T t) {
		try {
			dao.delete(t);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see pl.xs.xdms.core.services.CrudService#count()
	 */
	@Override
	public Long count(T t) {
		try {
			return dao.countAll();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
